cmake_minimum_required(VERSION 3.16)

set(CMAKE_C_COMPILER    clang)
set(CMAKE_CXX_COMPILER  clang++)

get_filename_component(SECURITY_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
get_filename_component(ROOT_DIR ${SECURITY_DIR} DIRECTORY)

set(CRYPTO_FRAMEWORK_DIR ${SECURITY_DIR}/crypto_framework)
set(CERTIFICATE_FRAMEWORK_DIR ${SECURITY_DIR}/certificate_framework)

set(OUTPUT_DIR ${ROOT_DIR}/output)
set(DEPEND_LIB ${OUTPUT_DIR}/lib)
set(DEPEND_INC ${OUTPUT_DIR}/include)

list(APPEND COMPILE_OPTIONS
  -O0 -g -Wall -Werror
  -Wno-format # warning: using 'public' format specifier annotation outside of os_log()/os_trace() [-Wformat]
  -fsanitize=address -fsanitize-recover=all -fno-omit-frame-pointer # asan
  -DOPENSSL_SUPPRESS_DEPRECATED # warning: 'HMAC_Init_ex' is deprecated [-Wdeprecated-declarations]
)

list(APPEND LINK_OPTIONS
  -fsanitize=address # asan
)

# library
add_library(LIB_COMPILE_CONFIG INTERFACE)
target_compile_options(LIB_COMPILE_CONFIG INTERFACE ${COMPILE_OPTIONS} --coverage -fPIC)
add_library(LIB_LINK_CONFIG INTERFACE)
target_link_options(LIB_LINK_CONFIG INTERFACE ${LINK_OPTIONS} --coverage)
target_link_libraries(LIB_LINK_CONFIG INTERFACE pthread ssl crypto boundscheck gcov)

# binary
add_library(EXE_COMPILE_CONFIG INTERFACE)
target_compile_options(EXE_COMPILE_CONFIG INTERFACE ${COMPILE_OPTIONS} -fPIE)
add_library(EXE_LINK_CONFIG INTERFACE)
target_link_options(EXE_LINK_CONFIG INTERFACE ${LINK_OPTIONS} -pie)
target_link_libraries(EXE_LINK_CONFIG INTERFACE pthread ssl crypto boundscheck gmock gtest gtest_main)

function(create_wrap_function target_name wrap_function_file)
  file(STRINGS ${wrap_function_file} WRAP_FUNCTIONS)
  add_library(${target_name} INTERFACE)
  target_link_options(${target_name} INTERFACE -Wl,-E)

  foreach(func IN LISTS WRAP_FUNCTIONS)
    if(func AND NOT func MATCHES "^#")
      target_link_options(${target_name} INTERFACE
        -Wl,--wrap=${func}
      )
    endif()
  endforeach()
endfunction()
